package com.fire.market.service.impl.impl;

import com.fire.component.extension.Assert;
import com.fire.market.promo.api.dto.PromoInfo;
import com.fire.market.manager.PromoManager;
import com.fire.market.model.PromoModel;
import com.fire.market.service.impl.PromoQueryCoreService;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PromoQueryCoreServiceImpl implements PromoQueryCoreService {
    @Resource
    PromoManager promoManager;
    @Override
    public PromoInfo queryPromoByItemId(Integer itemId) {
        Assert.notNull(itemId, "itemId is null");
        PromoModel promoModel = promoManager.queryPromoByItemId(itemId);
        return convertFromPromoModel(promoModel);
    }


    public PromoInfo convertFromPromoModel(PromoModel promoModel){
        if (promoModel == null){
            return null;
        }
        PromoInfo promoInfo = new PromoInfo();
        BeanUtils.copyProperties(promoModel, promoInfo);
        promoInfo.setStartDate(promoModel.getStartDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
        promoInfo.setEndDate(promoModel.getEndDate().toString(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")));
        return promoInfo;
    }
}
